/**
 *
 * 2007-11-2 上午11:09:49
 * @author xuhuan
 */

package edu.ccut.saturn.studio.wfg.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.utils.RunTime;
import edu.ccut.saturn.studio.wfg.wfgmodel.LogicKeyAttribute;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnAttribute;

public abstract class NodeLoader {
	
	private final Dimension size = new Dimension(86, 46);

	protected String getNodeAttributeValue(Node node, String key) {
		Node attribute = node.getAttributes().getNamedItem(key);

		if (attribute != null) {
			return attribute.getNodeValue();
		}

		return "";
	}

	protected String getNodeElementValue(Node node, String key) {
		NodeList childNodes = node.getChildNodes();

		for (int i = 0; i < childNodes.getLength(); ++i) {
			Node child = childNodes.item(i);
			if (child.getNodeName().equals(key)) {
				if (child.getFirstChild() != null) {
					return child.getFirstChild().getNodeValue();
				}

				return "";
			}
		}

		return "";
	}

	protected Node getChildNode(Node parentNode, String nodeKey) {
		NodeList children = parentNode.getChildNodes();

		for (int i = 0; i < children.getLength(); ++i) {
			Node child = children.item(i);

			if (child.getNodeName().equals(nodeKey)) {
				return child;
			}
		}

		return null;
	}

	protected List<Node> getChildNodes(Node parentNode, String nodeKey) {
		NodeList children = parentNode.getChildNodes();

		List<Node> childNodes = new ArrayList<Node>();

		for (int i = 0; i < children.getLength(); ++i) {
			Node child = children.item(i);

			if (child.getNodeName().equals(nodeKey)) {
				childNodes.add(child);
			}
		}

		return childNodes;
	}

	protected Point getLocation(Node node) {
		String x = this.getNodeAttributeValue(node, Shape.KEY_ATTRIBUTE_X);
		String y = this.getNodeAttributeValue(node, Shape.KEY_ATTRIBUTE_Y);

		int xInt = 0;
		int yInt = 0;
		try {
			xInt = Integer.parseInt(x);
			yInt = Integer.parseInt(y);
		} catch (NumberFormatException e) {
			// e.printStackTrace();
		}

		return new Point(xInt, yInt);
	}

	protected List<SaturnAttribute> getSaturnAttributeList(Node node,String nodeName,String childName,String method) {
		Node fomDataNode = this.getChildNode(node, nodeName);

		List<SaturnAttribute> datas = new ArrayList<SaturnAttribute>();

		if (fomDataNode != null) {
			List<Node> dataNodes = this.getChildNodes(fomDataNode,childName);

			for (Node dataNode : dataNodes) {
				datas.add(getSaturnAttribute(new ChildLoader(),dataNode,method));
			}
		}
		
		return datas;
	}
	
	// 自定义参与者
	protected LogicKeyAttribute getCustomDefineParticipant(Node node,String nodeName,String childName,String method){
		Node userNode = this.getChildNode(node, nodeName);
		
		LogicKeyAttribute customDefineParticipantAttribute = new LogicKeyAttribute();
		
		if(userNode != null){
			Node uNode = this.getChildNode(userNode, childName);
			
			customDefineParticipantAttribute = (LogicKeyAttribute) getSaturnAttribute(new ChildLoader(),uNode,method);
		}
		
		return customDefineParticipantAttribute;
	}
	
	private SaturnAttribute getSaturnAttribute (ChildLoader child,Node node,String method){
		return RunTime.getSaturnAttribute(child, node, method);
	}

	public abstract List<Shape> loadNodes(Element root);
	
	
	public Dimension getSize() {
		return size;
	}
}
